package com.base.util;

import java.io.InputStream;
import java.util.Properties;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.dbcp.BasicDataSourceFactory;

public class DataSourceHelper {
	
	private static DataSource dataSource = null;
	
	public static void initDataSource() throws Exception{
		if(dataSource == null){
			Properties prop = new Properties();
			InputStream inputStream = DataSourceHelper.class.getClassLoader().getResourceAsStream("com/config/db2.properties");
			prop.load(inputStream);
			dataSource = BasicDataSourceFactory.createDataSource(prop);
		}
	}
	
	public static DataSource getDataSource(){
		try {
			initDataSource();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dataSource;
	}
	
	public static BasicDataSource getBasicDataSource(){
		try {
			initDataSource();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return (BasicDataSource)dataSource;
	}
}
